package com.yunzhou.utils;

import weaver.general.BaseBean;
import weaver.general.GCONST;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author	Huangwm
 * @description	日志输出记录
 */
public class LogTool {
	
	private PrintWriter logPrint;
    private String logFile = "";
    private String logPath="";//日志文件路径
    private boolean systemlog = false;//是否写系统日记
    BaseBean log =null;
    static SimpleDateFormat newDf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd");
    public LogTool(){
    	
    }
    public LogTool(String logPath, boolean systemlog){
        this.logPath = logPath;
        this.systemlog = systemlog;
    }
    
	//生成日志路径
    private String getLogFile(){
        //获取当前系统路径
        String sysPath = GCONST.getRootPath();
        if (sysPath == null) {
            sysPath = System.getProperty("user.dir");
        }
        if (!"".equals(logPath)) {
            if (logPath.endsWith("/")) {
                sysPath += logPath+df.format(new Date())+".log";
            } else {
                sysPath += logPath+"/"+df.format(new Date())+".log";
            }
        } else {
            sysPath += "/log/dev/"+df.format(new Date())+".log";
        }
        return sysPath;
    }
   
    private synchronized void  newLog(Object msg,Throwable e){
    	log=new BaseBean();
        logFile = getLogFile();
        try{
            logPrint = new PrintWriter(new FileWriter(logFile, true), true);
            logPrint.println(newDf.format(new Date()) + ": " + msg);
            if(e!=null){
            	log.writeLog(msg.toString(),e);
            	e.printStackTrace(logPrint);
            }
            logPrint.flush();
            logPrint.close();
        }catch(IOException e2){
            try{
            	 File file=new File(logFile);
            	 if(!file.getParentFile().exists()) {  
                     //如果目标文件所在的目录不存在，则创建父目录  
                     if(file.getParentFile().mkdirs()){
                    	 file.createNewFile();
                     }
                 }  
                logPrint = new PrintWriter(new FileWriter(logFile, true), true);
                logPrint.println(newDf.format(new Date()) + ": " + msg);
                if(e!=null){
                	log.writeLog(msg.toString(),e);
                	e.printStackTrace(logPrint);
                }
            }catch(IOException ex){
            	log.writeLog("Log记录出错了",ex);
            }
        }finally{
        	if(logPrint!=null){
        		logPrint.flush();
        		logPrint.close();
        	}
        }
    }

    public void writeLog(Object msg) {
    	if(msg instanceof Exception){
    		newLog("",(Throwable)msg);
    	}else{
    		newLog(msg,null);	
    	}
    }
   
    public void writeLog(Object msg,Throwable e) {
    	if(msg instanceof Exception){
    		log.writeLog("非异常对象，不输出日记");
    	}else{
    		newLog(msg,e);
    	}
    }
    
    public static void main(String[] args) {
    	//打印日志
    	new LogTool("/log/assetlog/huangwm/Uf_newxz_rkppjlBaseService",false).writeLog("这里是测试的数据");
    	try {
			Double.parseDouble("aaa");	
		} catch (Exception e) {
			new LogTool("/log/assetlog/huangwm/Uf_newxz_rkppjlBaseService",false).writeLog(e);
		}
	}
  
}
